/*******************************************************************************
 * Copyright (c) 2010 Mikhail Garber.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License (LGPL)
 * which accompanies this distribution
 * 
 * Contributors:
 *     Mikhail Garber - initial API and implementation
 ******************************************************************************/
package plumber.component;

import java.lang.reflect.Method;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import plumber.common.Utils;


/**
 * a Component Manager capable to locate a Component as a bean in Spring context environment
 * @author mgarber
 *
 */
public class SpringComponentManager extends AbstractComponentManager implements ComponentManager, ApplicationContextAware {

	ApplicationContext context;
	
	@Override
	public Object find(String componentName, String methodName) {
		Object o = context.getBean(componentName);
		Utils.validatePresent(o, componentName);
		Method m = findMethod(methodName, o.getClass());
		Utils.validatePresent(m, methodName);
		return o;
	}

	@Override
	public void setApplicationContext(ApplicationContext context)
			throws BeansException {
		this.context = context;
		
	}

}
